package edu.scau.mis.controller;

import edu.scau.mis.core.controller.ResultEntity;
import edu.scau.mis.core.entity.InMessage;
import edu.scau.mis.core.entity.OutMessage;
import edu.scau.mis.core.redis.RedisCache;
import edu.scau.mis.core.service.FileUploadService;
import edu.scau.mis.rest.service.RestItemService;
import edu.scau.mis.sys.service.SysAdminService;
import edu.scau.mis.sys.service.SysRoleService;
import edu.scau.mis.sys.utils.HttpServletRequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class TestController {

    @Autowired
    RedisCache redisCache;

    @Autowired
    FileUploadService fileUploadService;

    @Autowired
    SysAdminService sysAdminService;

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    RestItemService restItemService;

    @Autowired
    HttpServletRequest request;

    @PostMapping("/test")
    public String index(@RequestPart(value = "photo") MultipartFile photo){
        String photoUrl = fileUploadService.uploadDocument(photo);
        System.out.println(photoUrl);
        return photoUrl;
    }

    @GetMapping("/test1")
    public ResultEntity test1(){
        HttpServletRequestUtils.getOpenid(request);

        return ResultEntity.success("你说成功了没");
    }

    @MessageMapping("/from-webSocket-message") // 接受消息
    @SendTo("/to-webSocket-message") // 发送消息
    public ResultEntity test2(InMessage<String> inMessage){
        OutMessage<String> outMessage = new OutMessage<String>();
        outMessage.setContent(inMessage.getContent());
        return null;
    }

    @GetMapping("/test3")
    public String test3(){
        return "authorize ADMIN or USER request";
    }

    /**
     * 获取用户登录信息
     * @param token 仅用于测试。实际有后端项目，不需要设置token参数
     * @return
     */
    @GetMapping(value = "/getInfo")
    public ResultEntity getInfo(@RequestParam("token") String token) {
        System.out.println(token);
        Map<String, Object> responseData = new HashMap<>();
        responseData.put("roles","admin");
        responseData.put("name","张三");
        responseData.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        return ResultEntity.success(responseData);
    }
}

